package gitee.derumin.teleport_compass.mixin;

import gitee.derumin.lib_for_derumin.api.features.ChunkDataTools;
import gitee.derumin.lib_for_derumin.logger.DeruminLogger;
import gitee.derumin.teleport_compass.waypoints.Waypoints;
import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Block.class)
public abstract class BlockMixin extends AbstractBlock {
    public BlockMixin(Settings settings) {
        super(settings);
    }

    @Inject(method = "afterBreak", at = @At("HEAD"))
    private void warnBreak(World world, PlayerEntity player, BlockPos pos, BlockState state, BlockEntity blockEntity, ItemStack tool, CallbackInfo ci){
        if (state.isOf(Blocks.LODESTONE)){
            Waypoints waypoints = (Waypoints) ChunkDataTools.getExtraData(world, pos, Waypoints.ID);
            if (waypoints != null && waypoints.hasWaypoint(pos)){
                DeruminLogger.getLogger().warn(
                        player.getName().getString()
                                + "摧毁了位于"
                                + pos.toShortString()
                                + "的路标'"
                                + waypoints.getWaypointName(pos)
                                + "'。",
                        DeruminLogger.AnnounceType.ALL);
            }
        }
    }
}
